//
//  WelfareForcaseController.m
//  MindBox
//
//  Created by MS on 16-1-14.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "WelfareForcaseController.h"
#import "Discover_WelfareCell.h"
#import "UIView+Frame.h"
#import "MyNavigationBar.h"
#import "MyNavigationItem.h"
#import "MBProgressHUD+MJ.h"
#import "LGAlertView.h"
@interface WelfareForcaseController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak) UITableView * tableView;
@property (nonatomic,weak) LGAlertView * alertView;
@property (nonatomic,strong)  NSMutableArray * totalLastTime;
@property (nonatomic,strong) NSTimer * timer;
@end

@implementation WelfareForcaseController

-(LGAlertView *)alertView
{
    if (_alertView == nil) {
        LGAlertView * view = [LGAlertView show];
        [[UIApplication sharedApplication].keyWindow addSubview:view];
        _alertView = view;
    }
    return _alertView;
}

-(NSMutableArray *)totalLastTime
{
    if (_totalLastTime==nil) {
        _totalLastTime = [NSMutableArray array];
    }
    return _totalLastTime;
}

-(void)setForecaseArray:(NSArray *)forecaseArray
{
    _forecaseArray = forecaseArray;
    
    [self.timer invalidate];
    self.timer = nil;
    [_totalLastTime removeAllObjects];
    NSInteger i = 0;
    for (Discover_Goods * goods in forecaseArray) {
        NSDictionary *dic = @{@"indexPath": [NSString stringWithFormat:@"%ld",i],@"lastTime": [NSString stringWithFormat:@"%@",goods.remain_time]};
        
        [self.totalLastTime addObject:dic];
        i++;
    }
    
    [self startTimer];
}

- (void)startTimer
{
   NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(refreshLessTime) userInfo:@"" repeats:YES];
    self.timer = timer;
   // 如果不添加下面这条语句，在UITableView拖动的时候，会阻塞定时器的调用
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
    
}


- (void)refreshLessTime
{
    NSUInteger time;
    for (int i = 0; i < _totalLastTime.count; i++) {
        
        time = [[[_totalLastTime objectAtIndex:i] objectForKey:@"lastTime"]integerValue];
        
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:[[[_totalLastTime objectAtIndex:i] objectForKey:@"indexPath"] integerValue] inSection:0];
        
       Discover_WelfareCell  *cell = (Discover_WelfareCell *)[_tableView cellForRowAtIndexPath:indexPath];
        cell.timeView.text = [self lessSecondToDay:--time];
     
        NSDictionary *dic = @{@"indexPath": [NSString stringWithFormat:@"%ld",indexPath.row],@"lastTime": [NSString stringWithFormat:@"%ld",time]};
        [_totalLastTime replaceObjectAtIndex:i withObject:dic];
    }
}

- (NSString *)lessSecondToDay:(NSUInteger)seconds
{
    NSString *time = nil ;
    NSUInteger day  = (NSUInteger)seconds/(24*3600);
    NSUInteger hour = (NSUInteger)(seconds%(24*3600))/3600;
    NSUInteger min  = (NSUInteger)(seconds%(3600))/60;
    NSUInteger second = (NSUInteger)(seconds%60);
    
    if (day>0) {
        
        time = [NSString stringWithFormat:@"%lu天后开始",day+1];
    }
    else {
      time = [NSString stringWithFormat:@"%02lu:%02lu:%02lu",hour,min,second];
    }

    return time;
    
}


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    Discover_WelfareCell  *mycell = (Discover_WelfareCell *)cell;
    
    NSInteger time = [[[_totalLastTime objectAtIndex:indexPath.row] objectForKey:@"lastTime"]integerValue];
    mycell.timeView.text =  [self lessSecondToDay:time];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNavBar];
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, self.view.height-64) style:UITableViewStylePlain];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate  =self;
    [self.view addSubview:tableView];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.forecaseArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    Discover_WelfareCell * cell =  [Discover_WelfareCell cellWithTableView:tableView];
    Discover_Goods * goods =self.forecaseArray[indexPath.row];
    goods.wel_type = YES;
    
    cell.goods = goods;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    self.alertView.title = @"活动尚未开始哦~";
    _alertView.alpha = 1;
    
    [UIView animateWithDuration:1.5 delay:1 options:0 animations:^{
        _alertView.alpha = 0;
        
    } completion:^(BOOL finished) {
//        [_alertView removeFromSuperview];
    }];
    
}

-(void)setNavBar
{
    
    MyNavigationItem * leftItem = [MyNavigationItem navigationItemWithImage:@"left_go" andHighlightImage:@"left_go" andTag:0 andTitle:nil addTarget:self action:@selector(leftBtnClick)];
    
    MyNavigationBar * navBar = [[MyNavigationBar alloc]initWithFrame:CGRectMake(0, 20, self.view.width, 44)];
    navBar.backgroundColor = [UIColor whiteColor];
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, navBar.frame.size.height-0.5, navBar.frame.size.width, 0.5)];
    lineView.backgroundColor =[UIColor grayColor];
    [navBar addSubview:lineView];
    
    [navBar setNavigationBarWithBgImageName:@"无" andTitle:@"即将开始"  andTitleView:nil  andLeftItems:@[leftItem] andRightItems:nil];
    [self.view addSubview:navBar];
}



-(void)leftBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;

}

-(void)dealloc
{
    
//    NSLog(@"WelfareForcaseController卸载");
}


@end
